#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <ncs_rpg>

#define VERSION		"BETA TEST"

new ThisSkillID;

new Float:SkillInterval, SkillDamage, SkillDeath;

new bool:bRoundEnd = false;

public Plugin:myinfo =
{
	name		= "[NCS RPG] Skill Poison Smoke",
	author		= "[RUS] DiviX",
	description	= "Skill Poison Smoke for NCS RPG",
	version		= VERSION,
	url			= "N/A"
};

public OnPluginStart()
{
	HookEvent("smokegrenade_detonate", Event_SmokegrenadeDetonated);
	HookEvent("round_start", Event_OnRoundStart);
	HookEvent("round_end", Event_OnRoundEnd);
}

public RPG_OnSkillsRegister()
{
	ThisSkillID = RegisterSkill("poisonsm", 5, 10, 10);
	// time = 20.0
	SkillInterval = GetConfigValueFloat("poisonsm", "interval", 1.0);
	SkillDamage = GetConfigValue("poisonsm", "damage", 2);
	SkillDeath = GetConfigValue("poisonsm", "death", 1);
}

public OnMapStart()
{
	bRoundEnd = false;
}

public Event_OnRoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
{
	bRoundEnd = true;
}

public Event_OnRoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	bRoundEnd = false;
}

public Event_SmokegrenadeDetonated(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	if(IsValidPlayer(client))
	{
		new level = GetSkillLevel(client, ThisSkillID);
		if(level > 0)
		{
			new Handle:datapack = CreateDataPack();
			WritePackCell(datapack, client);
			WritePackFloat(datapack, GetEventFloat(event, "x"));
			WritePackFloat(datapack, GetEventFloat(event, "y"));
			WritePackFloat(datapack, GetEventFloat(event, "z"));
			
			new Handle:hTimer_PoisonInterval = CreateTimer(SkillInterval, Timer_PoisonInterval, datapack, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
			
			CreateTimer(20.0, Timer_KillTimer, hTimer_PoisonInterval, TIMER_FLAG_NO_MAPCHANGE);
		}
	}
}

public Action:Timer_PoisonInterval(Handle:timer, Handle:datapack)
{
	if(!bRoundEnd)
	{
		ResetPack(datapack);
		new client = ReadPackCell(datapack);
		if(IsValidPlayer(client))
		{
			decl Float:smoke_pos[3], Float:pos[3];
			smoke_pos[0] = ReadPackFloat(datapack);
			smoke_pos[1] = ReadPackFloat(datapack);
			smoke_pos[2] = ReadPackFloat(datapack);
			
			new amount = GetSkillLevel(client, ThisSkillID)*SkillDamage, amount2;
			for(new victim = 1; victim <= MaxClients; victim++)
			{
				if(!IsValidPlayer(victim))
					continue;
				
				if(!IsPlayerAlive(victim))
					continue;
				
				if(GetClientTeam(client) == GetClientTeam(victim))
					continue;
				
				GetClientAbsOrigin(victim, pos);
				// GetVectorDistance
				if(GetDistanceBetween(smoke_pos, pos) <= 225.0)
				{
					amount2 = GetClientHealth(victim)-amount;
					if(amount2 < 1)
						if(SkillDeath)
							amount = GetClientHealth(victim);
						else
							amount = GetClientHealth(victim)-1;
					
					DealDamage(victim, amount, client, DMG_POISON, "weapon_smokegrenade");
				}
			}
			
			return Plugin_Continue;
		}
	}
	
	CloseHandle(datapack);
	return Plugin_Stop;
}

public Action:Timer_KillTimer(Handle:timer, Handle:hTimer_PoisonInterval)
{
	if(hTimer_PoisonInterval != INVALID_HANDLE)
	{
		KillTimer(hTimer_PoisonInterval);
		hTimer_PoisonInterval = INVALID_HANDLE;
	}
}

// thx ArsiRC
Float:GetDistanceBetween(Float:startvec[3], Float:endvec[3])
{
	return SquareRoot((startvec[0]-endvec[0])*(startvec[0]-endvec[0])+(startvec[1]-endvec[1])*(startvec[1]-endvec[1])+(startvec[2]-endvec[2])*(startvec[2]-endvec[2]));
}